package com.lpb.spring.test1.factory.support.classinit;

import com.lpb.spring.test1.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

import java.lang.reflect.Constructor;

/**
 * @author linpengbo
 * @date 2022/4/29
 */
public class CglibInstantiationStrategy implements InstantiationStrategy{
    @Override
    public Object initinstance(BeanDefinition definition, Constructor ct, Object[] args) {
        Enhancer en=new Enhancer();
        en.setSuperclass(definition.getClazz());
        en.setCallback(new NoOp() {
            @Override
            public int hashCode() {
                return super.hashCode();
            }
        });
        if(ct==null){
            return en.create();
        }
        return en.create(ct.getParameterTypes(),args);
    }
}
